{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "724223ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679281630,03a4c610-598d-44dc-89a7-c985add63e43', 'time_used': 286, 'confidence': 75.726, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': 'f17acae3c9f797b2d57bfb800fcc006d', 'face_rectangle': {'top': 47, 'left': 180, 'width': 27, 'height': 27}}], 'faces2': [{'face_token': '0a4c8c0c068514e1356e79c52fb8e509', 'face_rectangle': {'top': 36, 'left': 129, 'width': 46, 'height': 46}}], 'image_id1': 'XyRcN2HElYSntvISoYQFiQ==', 'image_id2': 'gF0imIIdA63e1OJ04UtksQ=='}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('cxk1.jpg', 'rb')\n",
    "f_02 = open('cxk2.jpg', 'rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "params = {\n",
    "    'api_key':'VkRJbERVcOtgbs-lkr47Ty5Q9ea-MjAx',\n",
    "    'api_secret':'n8XRUE2uyGygSQEBIskDtagcapsk8T9w',\n",
    "    'image_base64_1':img_01,\n",
    "    'image_base64_2':img_02,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99c927cd",
   "metadata": {},
   "source": [
    "# 2、人脸库管理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d60dbcb",
   "metadata": {},
   "source": [
    "## 2.1 创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f38a867b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "{'faceset_token': 'f6eb3ae27c9a3e02f944e714f7e3eb49', 'time_used': 177, 'face_count': 0, 'face_added': 0, 'request_id': '1679283321,8073d083-607b-4543-a399-e7aa15ac73a5', 'outer_id': '', 'failure_detail': []}\n"
     ]
    }
   ],
   "source": [
    " #encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "\n",
    "params = {\n",
    "    'api_key':'VkRJbERVcOtgbs-lkr47Ty5Q9ea-MjAx',\n",
    "    'api_secret':'n8XRUE2uyGygSQEBIskDtagcapsk8T9w',\n",
    "    'display_name':'广州南方学院1栋宿舍楼'\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    " print (response.status_code)\n",
    "print (response.json())\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "bb19e454",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'f6eb3ae27c9a3e02f944e714f7e3eb49',\n",
       " 'tags': '',\n",
       " 'time_used': 103,\n",
       " 'user_data': '',\n",
       " 'display_name': '广州南方学院1栋宿舍楼',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1679283764,244482bd-1a7a-40ea-8787-6db529204fd2',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库集合详情查询\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "\n",
    "params = {\n",
    "    'api_key':'VkRJbERVcOtgbs-lkr47Ty5Q9ea-MjAx',\n",
    "    'api_secret':'n8XRUE2uyGygSQEBIskDtagcapsk8T9w',\n",
    "    'faceset_token':'f6eb3ae27c9a3e02f944e714f7e3eb49'\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "ee64e50e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'time_used': 127, 'error_message': 'MISSING_ARGUMENTS: faceset_token, outer_id', 'request_id': '1679284449,8da5481a-0f7d-4e36-b77c-7d766b2537f7'}\n",
      "400\n"
     ]
    }
   ],
   "source": [
    "#encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "f = open(\"cxk.jpg\",\"rb\")\n",
    "\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\n",
    "    'api_key':'VkRJbERVcOtgbs-lkr47Ty5Q9ea-MjAx',\n",
    "    'api_secret':'n8XRUE2uyGygSQEBIskDtagcapsk8T9w',\n",
    "    'image_base64':img,\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print(response.json())\n",
    "print (response.status_code)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "244c1fa6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "\n",
    "params = {\n",
    "    'api_key':'VkRJbERVcOtgbs-lkr47Ty5Q9ea-MjAx',\n",
    "    'api_secret':'n8XRUE2uyGygSQEBIskDtagcapsk8T9w',\n",
    "    'faceset_token':'f6eb3ae27c9a3e02f944e714f7e3eb49'\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
